package com.earthquake.monitor_system.service.impl;

import com.auth0.jwt.JWT;
import com.earthquake.monitor_system.controller.resultType.Pagination;
import com.earthquake.monitor_system.controller.resultType.UserInfoType;
import com.earthquake.monitor_system.dao.UserDao;
import com.earthquake.monitor_system.domain.SeismicInformation;
import com.earthquake.monitor_system.domain.UserDomain;
import com.earthquake.monitor_system.service.ITF.IUser;
import com.earthquake.monitor_system.utils.jwt.TokenUtlis;
import com.earthquake.monitor_system.utils.successCode.ImageCaptchaParams;
import com.earthquake.monitor_system.utils.successCode.ImageCaptchaResult;
import com.earthquake.monitor_system.utils.successCode.ImageCaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserImpl implements IUser {
    @Autowired
    private UserDao userDao;
    @Override
    public List<UserDomain> getUser() {
        List<UserDomain> user = userDao.getUser();
        return user;
    }

    @Override
    public Boolean register(String username, String password, String phone) {
        if(isExistUserName(username)) {
            return false;
        }
        Boolean register = userDao.register(username, password, phone);
        return register;
    }

    @Override
    public List<UserInfoType>  login(String username, String password, String code) {
        List<UserInfoType> login = userDao.login(username, password);
        System.out.println(login.size());
        if(login.size()!=0){
            UserInfoType userInfoType = login.get(0);
            String token = TokenUtlis.genToken(userInfoType.getId().toString(), userInfoType.getUsername());
            userInfoType.setToken(token);
        }
        return login;
    }
    // 判断用户是否已经存在
    @Override
    public Boolean isExistUserName(String username) {
        Boolean result = userDao.isExistUserName(username);
        if(result) {
            return true;
        }
        return false;
    }
    @Override
    public ImageCaptchaResult successCode() throws IOException {
        ImageCaptchaResult base64Img = ImageCaptchaUtil.createBase64(new ImageCaptchaParams());
        return base64Img;
    }

    @Override
    public UserInfoType getById(int id) {
        UserInfoType userInfo = userDao.getById(id);
        return userInfo;
    }

    @Override
    public Object queryUserInfoList(Pagination pagination) {
        Map<String,Integer> list = new HashMap<>();
        int pageSize = pagination.getPageSize();
        int currentPage = pagination.getCurrentPage();
        list.put("startIndex",(currentPage-1)*pageSize);
        list.put("currentPage",currentPage);
        list.put("pageSize",pageSize);
        String UserInfoCount = userDao.getUserInfoCount();
        list.put("total", Integer.valueOf(UserInfoCount));
        List<UserDomain> UserInfoList = userDao.queryUserInfoList(list);
        Map<String,Object> list2 = new HashMap<>();
        list2.put("SeismicInformationList",UserInfoList);
        list2.put("pagination", list);
        return list2;
    }

    @Override
    public Boolean deleteById(String id) {
        Boolean s = userDao.deleteById(id);
        return s;
    }

    @Override
    public Boolean changePassword(String password, String userName, String phone) {
        Boolean aBoolean = userDao.changePassword(password, userName, phone);
        return aBoolean;
    }

    @Override
    public Boolean uploadAvatar(String avatar,String token) {
       String id = JWT.decode(token).getAudience().get(0);
        Boolean result= userDao.uploadAvatar(avatar,id);
        return result;
    }

    @Override
    public Boolean changeUserInfo(UserDomain UserDomain,String token) {
        String id = JWT.decode(token).getAudience().get(0);
        UserDomain.setId(Integer.valueOf(id));
        Boolean result = userDao.changeUserInfo(UserDomain);
        return result;
    }

}
